package com.bzkj.web.controller.ordermanagement.Inbound;

import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.purchaseinorder.dto.PurchaseInboundDTO;
import com.bzkj.document.service.purchase.PurchaseInService;
import com.bzkj.document.service.purchase.PurchaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ordermanagement
 * @className: PurchaseController
 * @description: 采购单据控制层
 * @author: C1309
 * @createDate: 2024年11月18日
 * @version: v1.0
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseInOrderController extends BaseController {

    /**
     * 采购订单据服务层接口
     */
    @Resource
    private PurchaseService purchaseService;

    /**
     * 采购入库单据服务层
     */
    @Resource
    private PurchaseInService purchaseInService;


    /**
     * 查询NCC采购订单据
     *
     * @return 采购订单据集合
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:purchase:list')")
    public TableDataInfo selectNccPurchaseOrder(@RequestParam HashMap<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        return purchaseService.selectPurchaseOrder(nccRequestDTO);
    }

    /**
     * 创建采购入库单
     *
     * @return 结果
     */
    @PostMapping("/purchase-inbound-order")
    @PreAuthorize("@ss.hasPermi('order:purchase:add')")
    @LogB(title = "采购入库单", businessType = BusinessType.INSERT)
    public AjaxResult insertPurchase(@RequestBody HashMap<String, Object> responseBody) {
        return purchaseService.insertPurchase(responseBody, getLoginUser());
    }

    /**
     * 查询采购入库单(WMS系统)
     *
     * @param purchaseInboundDTO 查询条件
     */
    @GetMapping("/purchase-inbound-order")
    @PreAuthorize("@ss.hasPermi('order:purchase:list')")
    public TableDataInfo selectPurchaseInboundOrder(PurchaseInboundDTO purchaseInboundDTO) {
        if (purchaseInboundDTO == null) {
            purchaseInboundDTO = new PurchaseInboundDTO();
        }
        // startPage();
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return purchaseInService.getPurchaseInOrder(purchaseInboundDTO, pageNum, pageSize);
    }

    /**
     * 删除采购入库单
     *
     * @param cgeneralhid 采购入库单头ID
     * @return 结果
     */
    @DeleteMapping("/purchase-inbound-order/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:purchase:delete')")
    @LogB(title = "采购入库单", businessType = BusinessType.INVALID)
    public AjaxResult deletePurchaseInboundOrder(@PathVariable String cgeneralhid, @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return purchaseInService.deletePurchaseInboundOrder(cgeneralhid, getLoginUser(), reason);
    }

    /**
     * 采购入库单签字确认
     *
     * @param cgeneralhid 采购入库单头ID
     * @return 签字结果
     */
    @PutMapping("/purchase-inbound-order/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:purchase:audit')")
    @LogB(title = "采购入库单", businessType = BusinessType.SIGN)
    public AjaxResult auditPurchaseInboundOrder(@PathVariable String cgeneralhid) {
        return purchaseInService.auditPurchaseInboundOrder(cgeneralhid, getLoginUser());
    }

    /**
     * 修改采购入库单
     *
     * @param responseBody 需要修改的采购入库单数据
     * @return 修改结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:purchase:edit')")
    @LogB(title = "采购入库单", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_in_material",
            detailTable = "wms_warehouse_in_detail_material"
    )
    public AjaxResult updatePurchase(@RequestBody HashMap<String, Object> responseBody) {
        return purchaseService.updatePurchase(responseBody, getLoginUser(), false);
    }

    /**
     * 采购入库单回退
     *
     * @param headerId 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:purchase:edit')")
    @LogB(title = "采购入库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return purchaseService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:purchase:audit')")
    @LogB(title = "采购入库单", businessType = BusinessType.INVENTORY)
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId){
        return purchaseService.updateInventory(headerId, getLoginUser());
    }
}
